package com.rattsoftware.server.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import com.rattsoftware.server.admin.model.Course;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.dao.CourseDAO;

/**
 * Provides a hibernate implementation
 * of the <code>UserDAO</code> data access <code> interface.</code>
 */
@Repository("courseDAO")
public class HibernateCourseDAO extends BaseDAOImpl<Course> implements CourseDAO {

	@Override
	public void deleteCourse(Course course) {
		remove(course);
	}

	@Override
	public Course getCourse(Integer id) {
		return super.find(id);
	}

	@Override
	public PagingLoadResult<Course> getCourseList(
			PagingLoadConfig loadConfig) {
		Search courseSearch = getSearchFromLoadConfig(loadConfig, "id");
		SearchResult<Course> result = searchAndCount(courseSearch);
		return getPagingLoadResult(loadConfig, result);
	}

	@Override
	public PagingLoadResult<Course> getCourseList(Integer deptId,
			PagingLoadConfig loadConfig) {
		Search courseSearch = getSearchFromLoadConfig(loadConfig, "id");
		courseSearch.addFilterEqual("deptId", deptId);
		SearchResult<Course> result = searchAndCount(courseSearch);
		return getPagingLoadResult(loadConfig, result);
	}

	@Override
	public List<Course> getCourses() {
		Search s = new Search();
		s.addSort("name", false);
		return search(s);
	}

	@Override
	public void saveCourse(Course course) {
		save(course);
	}
}